--env.setErrorMessageBoxEnabled(true)

-- 获取目标函数，通过传入一个前缀table来寻找目标，最后返回一个UNIT的table
function getTGTs( prefixes )
  local temp_set = SET_GROUP:New():FilterPrefixes(prefixes):FilterStart()
  local temp_units = SET_UNIT:New()
  temp_set:ForEachGroupAlive(
  function(group)
    local temp = group:GetUnits()
    for k,v in ipairs(temp) do
      temp_units:AddUnit(v)
    end
  end)
  return temp_units
end


local HQ = GROUP:FindByName( "GRIFFIN HQ" )
local CommandCenter = COMMANDCENTER:New( HQ ,"格里芬指挥中心")

Mission_South = MISSION:New(CommandCenter,"协助格里芬打击南部的各个目标", "中等",
    "请协助友机推进，打击任务指引的目标。",
    SIDE.BLUE)

local south_clients_prifixes = {"GRIFFIN CVN", "GRIFFIN 哈伊马角", "GRIFFIN 第一阶段", "GRIFFIN 迪拜", "GRIFFIN KUZ"}
Mission_South_clients = SET_GROUP:New():FilterCoalitions("blue"):FilterPrefixes(south_clients_prifixes):FilterStart()

-- 定义需要打击的目标集合
do
  -- 哈伊马角TGT
  Task_Khaimah_TGTs = SET_UNIT:New()
  local temp_units = GROUP:FindByName("IRONBLOOD GROUND BASE #002"):GetUnits()
  for k, v in ipairs(temp_units) do
    Task_Khaimah_TGTs:AddUnit(v)
  end
end

do
  -- 迪拜TGT
  local temp_prefixes = {"IRONBLOOD GROUND BASE #003", "IRONBLOOD SAM #001"}
  Task_Dubai_TGTs = getTGTs(temp_prefixes)
  if Task_Dubai_TGTs == nil then
    MESSAGE:New("Task_Udbai_TGTS is nil ", 10, "DEBUG", true):ToAll()
  end
  
--  Task_Dubai_TGTs = SET_UNIT:New()
--  local temp_prefixes = {"IRONBLOOD GROUND BASE #003", "IRONBLOOD SAM #001"}
--  local temp_units = SET_GROUP:New():FilterPrefixes(temp_prefixes):FilterStart()
--  temp_units:ForEachGroup(
--    function( group )
--      local temp_units = group:GetUnits()
--      for k, v in ipairs(temp_units) do
--        Task_Dubai_TGTs:AddUnit(v)
--      end
--    end
--  )
end

do
  -- 铁血前线基地TGT
  local temp_prefixes = {"IRONBLOOD FRONTBASE #001", "IRONBLOOD Support Boat #001"}
  Task_South_FB_TGTs = getTGTs(temp_prefixes)
--  Task_South_FB_TGTs = SET_UNIT:New()
--  local temp_units = SET_GROUP:New():FilterPrefixes(temp_prefixes):FilterStart()
--  temp_units:ForEachGroup(
--    function( group )
--      local temp_units = group:GetUnits()
--      for k, v in ipairs(temp_units) do
--        Task_South_FB_TGTs:AddUnit(v)
--      end
--    end
--  )
end



-- 指定Task
-- *************
-- 哈伊马角TASK
local Task_Khaimah = TASK_A2G_BAI:New(
Mission_South,
Mission_South_clients,
"进攻哈伊马角机场",
Task_Khaimah_TGTs,
"进攻哈伊马角机场，清除所有可能的威胁并且降落至该机场完成占领。")

-- 迪拜TASK
local Task_Dubai = TASK_A2G_BAI:New(
Mission_South,
Mission_South_clients,
"进攻迪拜机场",
Task_Dubai_TGTs,
"进攻迪拜机场，清除所有可能的威胁并且降落至该机场完成占领。")

-- FB TASK
local Task_South_FB = TASK_A2G_BAI:New(
Mission_South,
Mission_South_clients,
"空袭前线基地",
Task_South_FB_TGTs,
"空袭任务指引的一处铁血前线基地，清除所有可能的威胁。")

Mission_South:Start()
